<?php
require_once(dirname(__FILE__) . '/app.php');

$view = "reg";

if ( $_POST ) {
	$u = array();
	$u['family_name'] = strval($_POST['familyName']);
	$u['personal_name'] = strval($_POST['personalName']);
	
	$u['email'] = strval($_POST['email']);
	$u['tel_no'] = strval($_POST['mobile']);
	
	$u['password'] = strval($_POST['password']);
	$u['sex'] = abs(intval($_POST['sex']));
	
	if( $_POST['back'] ){
		include template($view);
		exit;
	}

	if ( ! Utility::ValidEmail($u['email'], true) ) {
		Session::Set('error', 'Email地址为无效地址');
		redirect( WEB_ROOT . '/reg.php');
	}
	$user = Users::GetUserByEmail($u['email']);
	if ( $user ){
		Session::Set('error', '注册失败，Email已被使用');
	}
	
	
	if ($_POST['password']) {
		if ( $user_id = Users::Create($u) ) {
			
			//ZLogin::Login($user_id);
			$u = Users::GetUser($user_id);
			Mailer::SmtpMail($u['email'], "test", "http://test.elegantre-sort.com/reservation/active.php?iid=".$u['secret']);
			//Mailer::SmtpMail("info@afar.co.jp", $u['email'], "test", "http://localhost/mapreengage/active.php?iid".$u['secret']);
			//mail($u['email'], "test", "http://localhost/mapreengage/active.php?iid".$u['secret']);
			$view = "regConfirmOk";
			//redirect(get_loginpage(WEB_ROOT . '/index.php'));
		} else {
			$au = Users::GetUserByEmail($_POST['email']);
			if ( $au ) {
				Session::Set('error', '注册失败，Email已被使用');
			} else {
				Session::Set('error', '注册失败，用户名已被使用');
			}
		}
	} else {
		Session::Set('error', '注册失败，密码设置有问题');
	}
}
include template($view);

